#ifndef ZFILE_TOOLS_H_
#define ZFILE_TOOLS_H_

#include "../Common/GlobalDefines.h"
#include <cstdio>
#ifdef _WIN64
#   include <direct.h>  // for _mkdir()
#endif

GLOBAL_NAMESPACE_BEGIN

namespace FileTools
{
    static bool copyFile(const char* source, const char* target)
    {
        FILE *fin;
        FILE *fout;
        char buffer[256];
        if ((fin = fopen(source, "rb")) != NULL
            && (fout = fopen(target, "wb+")) != NULL)
        {
            while (!feof(fin))
            {
                fread(buffer, sizeof(char) * 256, 1, fin);
                fwrite(buffer, sizeof(char) * 256, 1, fout);
            }
            rewind(fout);
            fclose(fin);
            fclose(fout);
            return true;
        }
        else
        {
            return false;
        }

    }

    // check the dir, if it doesn't exist, create it
    // return 0, if it exists
    // return 1, create success
    // return -1. create fails
    static int checkDir(const char* dir)
    {
        FILE *fp = NULL;
        char tmpDir[200];
        memset(tmpDir, '\0', sizeof(tmpDir));
        sprintf(tmpDir, dir);
        strcat(tmpDir, "\\");
        strcat(tmpDir, ".temp.fortest");
        fp = fopen(tmpDir, "w");
        if (!fp)
        {
            if (_mkdir(dir) == 0)
                return 1;
            else
                return -1;
        }
        else
            fclose(fp);
        return 0;
    }
}

GLOBAL_NAMESPACE_END

#endif//ZFILE_TOOLS_H_